package com.linxianqin.springbootVue.controller.system;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.linxianqin.springbootVue.domain.constant.UserConstants;
import com.linxianqin.springbootVue.domain.convert.SysDictTypeConvert;
import com.linxianqin.springbootVue.domain.dto.SysDictTypeDTO;
import com.linxianqin.springbootVue.domain.dto.SysRoleDTO;
import com.linxianqin.springbootVue.domain.enums.BusinessType;
import com.linxianqin.springbootVue.domain.model.AjaxResult;
import com.linxianqin.springbootVue.domain.page.TableDataInfo;
import com.linxianqin.springbootVue.domain.query.SysDictTypeQuery;
import com.linxianqin.springbootVue.manage.annotation.SysLog;
import com.linxianqin.springbootVue.manage.core.BaseController;
import com.linxianqin.springbootVue.service.SysDictTypeService;
import com.linxianqin.springbootVue.utils.SecurityUtils;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author lxq
 * @version 1.0
 * @description: 数据字典信息
 * @date 2020/12/31 11:06
 */
@Slf4j
@RestController
@RequestMapping("/system/dict/type")
public class SysDictTypeController extends BaseController {
    private final SysDictTypeService dictTypeService;

    public SysDictTypeController(SysDictTypeService dictTypeService){
        this.dictTypeService=dictTypeService;
    }
    @PreAuthorize("@ss.hasPermi('system:dict:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysDictTypeQuery query)
    {
        log.info("查询的信息：{}",query);
        //构建分页bean
        Page<SysDictTypeDTO> page = buildPage();
        //查询分页数据
        Page<SysDictTypeDTO> dtoPage = dictTypeService.selectSysDictTypePage(page, query);
        return getDataTable(dtoPage);
    }

    /**
     * 查询字典类型详细
     */
    @PreAuthorize("@ss.hasPermi('system:dict:query')")
    @GetMapping(value = "/{dictId}")
    public AjaxResult getInfo(@PathVariable Long dictId)
    {
        return AjaxResult.success(SysDictTypeConvert.INSTANCE.entity2dto(dictTypeService.getById(dictId)));
    }

    /**
     * 删除字典类型
     */
    @PreAuthorize("@ss.hasPermi('system:dict:remove')")
    @SysLog(title = "字典类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dictIds}")
    public AjaxResult remove(@PathVariable List<Long> dictIds)
    {
        return toAjax(dictTypeService.removeByIds(dictIds));
    }

    /**
     * 新增字典类型
     */
    @PreAuthorize("@ss.hasPermi('system:dict:add')")
    @SysLog(title = "字典类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysDictTypeDTO dictTypeDTO)
    {
        if (UserConstants.NOT_UNIQUE.equals(dictTypeService.checkDictTypeUnique(dictTypeDTO)))
        {
            return AjaxResult.error("新增字典'" + dictTypeDTO.getDictName() + "'失败，字典类型已存在");
        }
        dictTypeDTO.setCreateBy(SecurityUtils.getUsername());
        return toAjax(dictTypeService.save(SysDictTypeConvert.INSTANCE.dto2entity(dictTypeDTO)));
    }

    /**
     * 修改字典类型
     */
    @PreAuthorize("@ss.hasPermi('system:dict:edit')")
    @SysLog(title = "字典类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysDictTypeDTO dictTypeDTO)
    {
        if (UserConstants.NOT_UNIQUE.equals(dictTypeService.checkDictTypeUnique(dictTypeDTO)))
        {
            return AjaxResult.error("修改字典'" + dictTypeDTO.getDictName() + "'失败，字典类型已存在");
        }
        dictTypeDTO.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(dictTypeService.updateById(SysDictTypeConvert.INSTANCE.dto2entity(dictTypeDTO)));
    }

    /**
     * 清空缓存
     */
    @PreAuthorize("@ss.hasPermi('system:dict:remove')")
    @SysLog(title = "字典类型", businessType = BusinessType.CLEAN)
    @DeleteMapping("/clearCache")
    public AjaxResult clearCache()
    {
        dictTypeService.clearCache();
        return AjaxResult.success();
    }


    /**
     * 获取字典选择框列表
     */
    @GetMapping("/optionselect")
    public AjaxResult optionselect()
    {
        List<SysDictTypeDTO> dictTypes = SysDictTypeConvert.INSTANCE.entity2dto(dictTypeService.list());
        return AjaxResult.success(dictTypes);
    }
}
